package com.wuliu.system.service.impl;

import java.util.List;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wuliu.common.constant.CacheConstants;
import com.wuliu.common.core.redis.RedisCache;
import com.wuliu.common.exception.ServiceException;
import com.wuliu.common.utils.DateUtils;

import javax.annotation.Resource;

import com.wuliu.common.utils.SecurityUtils;
import com.wuliu.common.utils.StringUtils;
import com.wuliu.common.utils.ThreadLocalContext;
import com.wuliu.system.domain.dto.AddBasCustDto;
import org.springframework.stereotype.Service;
import com.wuliu.system.mapper.BasCustMapper;
import com.wuliu.common.core.domain.entity.BasCust;
import com.wuliu.system.service.IBasCustService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 微信用户Service业务层处理
 *
 * @author realue
 * @date 2023-10-09
 */
@Service
public class BasCustServiceImpl extends ServiceImpl<BasCustMapper, BasCust> implements IBasCustService {
    @Resource
    private BasCustMapper basCustMapper;
    @Resource
    private RedisCache redisCache;

    /**
     * 查询微信用户
     *
     * @param custNo 微信用户主键
     * @return 微信用户
     */
    @Override
    public BasCust selectBasCustByCustNo(Integer custNo) {
        return basCustMapper.selectBasCustByCustNo(custNo);
    }

    /**
     * 查询微信用户列表
     *
     * @param basCust 微信用户
     * @return 微信用户
     */
    @Override
    public List<BasCust> selectBasCustList(BasCust basCust) {
        return basCustMapper.selectBasCustList(basCust);
    }

    /**
     * 新增微信用户
     *
     * @param basCust 微信用户
     * @return 结果
     */
    @Override
    public int insertBasCust(BasCust basCust) {
        basCust.setSysAccount(SecurityUtils.getLoginUser().getUser().getSysAccount());
        long count = basCustMapper.selectCount(new LambdaQueryWrapper<BasCust>()
                .eq(BasCust::getSysAccount, basCust.getSysAccount())
                .eq(BasCust::getLoginNo, basCust.getLoginNo()));
        if (count > 0) {
            throw new ServiceException("登录账号已存在");
        }
        if (StringUtils.isNotEmpty(basCust.getPassword())) {
            basCust.setPassword(SecurityUtils.encryptPassword(basCust.getPassword()));
        }
        basCust.setCreateTime(DateUtils.getNowDate());
        return basCustMapper.insertBasCust(basCust);
    }

    /**
     * 修改微信用户
     *
     * @param basCust 微信用户
     * @return 结果
     */
    @Override
    public int updateBasCust(BasCust basCust) {
        basCust.setUpdateTime(DateUtils.getNowDate());
        int cnt = basCustMapper.updateBasCust(basCust);
        //刷新缓存
        if (cnt > 0) {
            BasCust cust = basCustMapper.selectOne(new LambdaQueryWrapper<BasCust>()
                    .eq(BasCust::getId, basCust.getId()));
            String token = (String) ThreadLocalContext.getCurrentToken();
            redisCache.setCacheObject(CacheConstants.TOKEN_WEIXIN_LOGIN_INFO_KEY + token, JSONUtil.toJsonStr(cust), 7, TimeUnit.DAYS);
        }
        return cnt;
    }

    /**
     * 批量删除微信用户
     *
     * @param custNos 需要删除的微信用户主键
     * @return 结果
     */
    @Override
    public int deleteBasCustByCustNos(Integer[] custNos) {
        return basCustMapper.deleteBasCustByCustNos(custNos);
    }

    /**
     * 删除微信用户信息
     *
     * @param custNo 微信用户主键
     * @return 结果
     */
    @Override
    public int deleteBasCustByCustNo(Integer custNo) {
        return basCustMapper.deleteBasCustByCustNo(custNo);
    }

    /**
     * 重置密码
     *
     * @param newPwd
     * @param custId
     * @return
     */
    @Override
    public Boolean resetPwd(String newPwd, String custId) {
        BasCust cust = new BasCust();
        cust.setId(custId);
        cust.setUpdateTime(DateUtils.getNowDate());
        cust.setPassword(SecurityUtils.encryptPassword(newPwd));
        return basCustMapper.updateBasCust(cust) > 0;
    }

    /**
     * @param dto
     * @return
     */
    @Override
    public Boolean insertBasCust(AddBasCustDto dto) {
        BasCust basCust = new BasCust();
        BeanUtil.copyProperties(dto, basCust);
        basCust.setCreateTime(DateUtils.getNowDate());
        return insertBasCust(basCust) > 0;
    }
}
